package com.diaohw.platform.framework.nacos;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.exception.NacosException;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 刁海文
 * @Remark
 * @since 2023/6/16 18:37
 */
@Service
public class NacosManager {
    @Resource
    NacosServiceManager nacosServiceManager;
    @Resource
    DiscoveryClient discoveryClient;
    @Resource
    private
    NacosConfigManager nacosConfigManager;

    /**
     * 在线服务
     *
     * @return 在线的服务名称
     */
    public List<String> services() {
        return discoveryClient.getServices();
    }

    /**
     * 目标服务是否在线
     *
     * @param record 服务Enum
     * @return true:在线, false:离线
     */
    public boolean isOnLine(ServiceRecord record) {
        return services().contains(record.serviceId());
    }

    /**
     * 在线的服务实例
     *
     * @param record 服务
     */
    public List<ServiceInstance> instances(ServiceRecord record) {
        return discoveryClient.getInstances(record.serviceId());
    }

    public void shutdown() throws NacosException {
        nacosServiceManager.nacosServiceShutDown();
    }
}
